package com.ui.zhong.webview

import android.content.Context
import android.content.Intent
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.fragment.app.Fragment
import com.google.auto.service.AutoService
import com.ui.zhong.Constant
import com.ui.zhong.webview.WebViewFragment.Companion.newInstance
import com.walt.zhong.base.BaseApplication
import com.walt.zhong.common.webview.IWebViewService

@AutoService(IWebViewService::class)
class WebViewImpl : IWebViewService{
    override fun startWebViewActivity(
        context: Context,
        url: String,
        title:String,
        showActionBar: Boolean,
        needRefresh: Boolean
    ) {
        val intent = Intent(context,WebViewActivity::class.java)
        intent.putExtra(Constant.WEB_VIEW_ACTIVITY_URL,url)
        intent.putExtra(Constant.WEB_VIEW_ACTIVITY_SHOW_ACTION_BAR,showActionBar)
        intent.putExtra(Constant.WEB_VIEW_ACTIVITY_TITLE,title)
        intent.putExtra(Constant.WEB_VIEW_ACTIVITY_NEED_REFRESH,needRefresh)
        context.startActivity(intent)

    }

    override fun getWebViewFragment(url: String, needRefresh: Boolean): Fragment {
        return newInstance(url,needRefresh)
    }

    @Composable
    override fun ComposeWebView(url: String,title: String, needRefresh: Boolean) {
        val mutableTitle = remember { mutableStateOf(title) }
        return InternalComposeWebView(url, mutableTitle,needRefresh)
    }

    override fun openLocalWebPage(context: Context) {
        val intent = Intent(context,WebViewActivity::class.java)
        intent.putExtra(Constant.WEB_VIEW_ACTIVITY_URL,"file:///android_asset/demo.html")
        context.startActivity(intent)
    }
}